﻿namespace Nello.Web.Controls
{
    using Nello;
    using Nello.Web;
    using System;
    using System.ComponentModel;
    using System.Web.UI.WebControls;

    public class NDropDownList : DropDownList, IDataControl
    {
        private bool addDefault = false;
        private Nello.DataType datatype = Nello.DataType.String;
        private bool isignore = false;

        public void DoDataBind(object datasource)
        {
            this.DataSource = datasource;
            this.DataBind();
            if (this.AddDefault)
            {
                ListItem item = new ListItem("请选择", "-1");
                this.Items.Insert(0, item);
            }
        }

        public void DoDataBind(object datasource, string dataTextField, string dataValueField)
        {
            this.DataTextField = dataTextField;
            this.DataValueField = dataValueField;
            this.DoDataBind(datasource);
        }

        public bool AddDefault
        {
            get
            {
                return this.addDefault;
            }
            set
            {
                this.addDefault = value;
            }
        }

        public Nello.DataType DataType
        {
            get
            {
                return this.datatype;
            }
            set
            {
                this.datatype = value;
            }
        }

        [Browsable(false)]
        public object DataValue
        {
            get
            {
                return DataTypeUtil.GetValue(this.datatype, this.SelectedValue);
            }
            set
            {
                if (value == null)
                {
                    this.SelectedIndex = -1;
                }
                else
                {
                    this.SelectedValue = value.ToString();
                }
            }
        }

        public bool IsIgnore
        {
            get
            {
                return this.isignore;
            }
            set
            {
                this.isignore = value;
            }
        }
    }
}

